package com.woniu.shop.task;


import com.woniu.shop.utils.QiNiuUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

@Configuration
@Slf4j
public class DeleteRedisImageTask {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 0 0/2 * * ?")
    public void clearImage(){
        log.debug("=====清除无效图片定时任务开始=====");
        // 计算Redis中两个set集合的差值
        Set<String> difference = stringRedisTemplate.opsForSet().difference(
                "redis_all_upload_image",
                "redis_db_upload_image");
        if(difference != null && difference.size() > 0){
            for(String fileName : difference){
                log.debug("=====删除无效图片: " + fileName + "=====");
                QiNiuUploadUtils.deleteFile(fileName);
                // 删除Redis最中的值
                //stringRedisTemplate.opsForSet().remove(redisentity.REDIS_ALL_UPLOAD_IMAGE, fileName);
                stringRedisTemplate.opsForSet().remove("redis_all_upload_image",fileName);
            }
        }
        log.debug("=====清除无效图片定时任务结束=====");
    }
}